package com.flashcash.solu.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.flashcash.kotlin.utils.FirebaseAnalyticsConstants;
import com.flashcash.kotlin.utils.FirebaseAnalyticsUtils;
import com.flashcash.kotlin.widget.LocalButton;
import com.flashcash.solu.R;
import com.flashcash.solu.bean.PersonalInfoServerBean;
import com.flashcash.solu.common.TokenManager;
import com.flashcash.solu.enums.TDChannel;
import com.flashcash.solu.view.camera.FaceDetectorFragment;
import com.flashcash.solu.view.certification.FacebookAccountActView;
import com.flashcash.solu.view.certification.presenter.FacebookAccountActPresenter;
import com.flashcash.solu.view.certification.presenter.FacebookAccountActPresenterImp;
import com.flashcash.solu.widget.xleotoast.XLeoToast;
import com.hwangjr.rxbus.RxBus;

import butterknife.OnClick;
import cn.fraudmetrix.octopus.aspirit.bean.OctopusParam;
import cn.fraudmetrix.octopus.aspirit.main.OctopusManager;

/**
 * Created on 2019-05-26 22:02.
 */
public class FacebookAccountFragment extends FaceDetectorFragment<FacebookAccountActPresenter> implements FacebookAccountActView {

    private static final String KEY_FROM_MAIN_ACTIVITY = "from_main_activity";

    private OctopusParam param;

    ImageView mIvRightIcon;
    TextView mIdTextviewTitle;
    LocalButton mBtnContinue;

    private boolean fromMainActivity;//从MainActivity跳转过来

    public static FacebookAccountFragment newInstance(boolean fromMainActivity) {
        Bundle args = new Bundle();
        args.putBoolean(KEY_FROM_MAIN_ACTIVITY, fromMainActivity);
        FacebookAccountFragment fragment = new FacebookAccountFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void submitButtonStateUpdate(boolean active) {

    }

    @Override
    protected FacebookAccountActPresenter initPresenter() {
        return new FacebookAccountActPresenterImp();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_facebook_account;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        mIvRightIcon = view.findViewById(R.id.id_imagebutton_info_list);
        mIdTextviewTitle = view.findViewById(R.id.id_textview_title);
        mBtnContinue = view.findViewById(R.id.btn_continue);


        String topTitle = getResources().getText(R.string.text_title_checkin_facebook).toString();
        mIdTextviewTitle.setText(topTitle);

        mIvRightIcon.setVisibility(View.GONE);

        //用户身份信息
        mPresenter.getPersonalInfo();
        //已认证的第三方账号
        mPresenter.getThirdPartAccount();

        initEvent();
    }

    private void initEvent() {
        RxBus.get().register(this);

        mBtnContinue.setOnClickListener(view -> {
            FirebaseAnalyticsUtils.INSTANCE.logEvent(FirebaseAnalyticsConstants.Event.BTN_VERIFICATION_SUBMIT, null);
            if (!fromMainActivity && getActivity() != null) {//从certify跳转过来
                getActivity().setResult(10010, getActivity().getIntent());
                getActivity().finish();
            } else {//跳转到活体检测页面
                showFaceDetectorDialog();
            }
        });
    }

    @OnClick(R.id.rl_facebook)
    public void onClick() {
        FirebaseAnalyticsUtils.INSTANCE.logEvent(FirebaseAnalyticsConstants.Event.CHECKBOX_VERIFICATION_FACEBOOK, null);
        openChannel(TDChannel.FACEBOOK);
    }

    @OnClick(R.id.rl_lazada)
    public void onClickLazada() {
        FirebaseAnalyticsUtils.INSTANCE.logEvent(FirebaseAnalyticsConstants.Event.CHECKBOX_VERIFICATION_LAZADA, null);
        openChannel(TDChannel.LAZADA);
    }

    @OnClick(R.id.id_imagebutton_back)
    public void onClickBack() {
        if (getActivity() != null) {
            getActivity().finish();
        }
    }

    /**
     * 打开认证页面
     */
    public void openChannel(TDChannel channel) {
        if (param == null) {
            XLeoToast.showMessage(getString(R.string.show_get_personalinfo_failed));
            return;
        }
        if (null == getActivity()) {
            return;
        }
        OctopusManager.getInstance().getChannel(getActivity(), channel.getValue(), param, (code, taskId) -> {
            String msg = code == 0 ? "success:" + taskId : "failure：" + code;
//            Log.i(TAG, "openChannel: " + taskId);
            if (code == 0) {
                //提交认证数据
                mPresenter.submitThirdPartData(channel.name(), "TONGDUN", channel.name(), taskId, param.identityCode, param.realName, param.mobile, "");
            } else {
                XLeoToast.showMessage(getString(R.string.account_verification_failed));
            }
        });
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            fromMainActivity = (boolean) getArguments().get(KEY_FROM_MAIN_ACTIVITY);
        }
    }

    @Override
    public void changeStatus(String type) {

    }

    @Override
    public void setPersonalInfo(PersonalInfoServerBean personalInfoServerBean) {
        if (personalInfoServerBean != null) {
            param = new OctopusParam();
            param.realName = personalInfoServerBean.getFullName();
            param.identityCode = personalInfoServerBean.getCredentialNo();
            param.mobile = TokenManager.getInstance().getMobile() == null ? personalInfoServerBean.getFullName() : TokenManager.getInstance().getMobile();
        }
    }
}
